package kadledav.portlet.insert;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import kadledav.backend.dto.NewsDTO;
import kadledav.backend.service.NewsService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

import com.liferay.portal.kernel.captcha.CaptchaException;
import com.liferay.portal.kernel.captcha.CaptchaUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.PortletURLFactoryUtil;
import static kadledav.portlet.insert.InsertNewsConstants.*;

@Controller
@RequestMapping("VIEW")
public class InsertNewsPortlet {

	private static final Logger logger = Logger.getLogger(InsertNewsPortlet.class);

	@Autowired
	private NewsService newsService;

	@RenderMapping
	public ModelAndView defaultRenderer(RenderRequest request) {
		logger.info("Insert news defautl renderer");
		ModelAndView modelAndView = new ModelAndView(JSP_VIEW);
		modelAndView.addObject(LAST_MSG, request.getPortletSession().getAttribute(LAST_MSG_WHEN_CAPTCHA_FAILED));
		return modelAndView;
	}

	@RequestMapping(params = "action=insertNews")
	public void sendNewsIdEvent(final ActionRequest request, final ActionResponse response, @RequestParam String newsText) throws IOException {
		logger.info("inserting new message");
		try {
			CaptchaUtil.check(request);
			logger.debug("captcha ok");
			request.getPortletSession().setAttribute(LAST_MSG_WHEN_CAPTCHA_FAILED, null);
			logger.debug("msg -" + newsText);
			NewsDTO newsDTO = new NewsDTO();
			newsDTO.setMessage(newsText);
			newsService.addNews(newsDTO);
			logger.info("inser complete: " + newsText);
		} catch (CaptchaException e) {
			request.getPortletSession().setAttribute(LAST_MSG_WHEN_CAPTCHA_FAILED, newsText);
			logger.debug("captcha failed");
		}
		String portletName = (String) request.getAttribute(WebKeys.PORTLET_ID);
		ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
		PortletURL redirectURL = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(request), portletName, themeDisplay.getLayout().getPlid(),
				PortletRequest.RENDER_PHASE);
		response.sendRedirect(redirectURL.toString().replaceAll(Pattern.quote("?") + ".*", ""));

	}

	@ResourceMapping
	public void getCapcha(ResourceRequest request, ResourceResponse response) throws IOException {
		CaptchaUtil.serveImage(request, response);
	}

}
